import pandas as pd

"""读取mining_result/result.csv，按照Binding_Score从大到小排序，然后另存为mining_result/filtered_result.csv
然后筛选出Binding_Score最大的n个记录，按照fasta格式，另存为mining_result/top_n_result.fa
"""

# 读取CSV文件
df = pd.read_csv('mining_result/result.csv')

# 按照Binding_Score从大到小排序
df_sorted = df.sort_values(by='Binding_Score', ascending=False)

# 另存为filtered_result.csv
df_sorted.to_csv('mining_result/filtered_result.csv', index=False)

# 筛选出Binding_Score最大的n个记录
n = 10  # 这里假设n为10，你可以根据需要修改n的值
top_n_df = df_sorted.head(n)

# 将筛选结果按照fasta格式保存
with open('mining_result/top_n_result.fa', 'w') as f:
    for index, row in top_n_df.iterrows():
        fasta_header = f">{row['Sequence_ID']}_{row['Binding_Score']}\n"  # 假设有一个Sequence_ID列
        fasta_sequence = f"{row['Sequence']}\n"  # 假设有一个Sequence列
        f.write(fasta_header)
        f.write(fasta_sequence)